#include "CGameManager.h"
#include "HelloWorldScene.h"
#include "CSplashScene.h"
#include "Constants.h"
#include "CGameOverScene.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "ADUIntroductionScene.h"

USING_NS_CC;

using namespace CocosDenshion;

CGameManager *CGameManager::_pInstance = NULL;

CGameManager *CGameManager::Instance() {
	if (!_pInstance) {
		_pInstance = new CGameManager;
	}

	return _pInstance;
}

void CGameManager::RunScene(int scene) {
	CCScene *pScene = NULL;

	if (scene == SCENE_GAME) {
		pScene = HelloWorld::scene();
	} else if (scene == SCENE_SPLASH) {
		pScene = CSplashLayer::scene();
	} else if (scene == SCENE_GAMEOVER) {
		// Do something here
		pScene = CGameOverLayer::scene();
	} else if (scene == SCENE_INTRO) {
		// Do something here
		pScene = ADUIntroductionLayer::scene();
	}

	if (CCDirector::sharedDirector()->getRunningScene() == NULL) {
		CCDirector::sharedDirector()->runWithScene(pScene);
	} else {
		CCScene* newScene = CCTransitionCrossFade::create(0.5, pScene);
		if(newScene)
		{
			CCDirector::sharedDirector()->replaceScene(newScene);
		}
	}
}

//void CGameManager::changeSoundState(int curentState)
//{
//	if( curentState == SOUND_ON)
//	{
//	    this->setSoundState(SOUND_OFF);
//	    this->stopSound();
//	}
//	else if(curentState == SOUND_OFF)
//	{
//	    this->setSoundState(SOUND_ON);
//	    this->playSound();
//	}
//}
void CGameManager::setSoundState(int curentState)
{
	CCUserDefault *def = CCUserDefault::sharedUserDefault();
	def->setIntegerForKey(SOUND_STATE, curentState);
	def->flush();
}

int CGameManager::getSoundState()
{
	return CCUserDefault::sharedUserDefault()->getIntegerForKey(SOUND_STATE);
}

void CGameManager::playSound()
{
	SimpleAudioEngine::sharedEngine()->playBackgroundMusic(BACKGROUND_MUSIC, true);
}

void CGameManager::playSound(const char* fileName) {
//	if(CGameManager::Instance()->getSoundState())
//	{
	SimpleAudioEngine::sharedEngine()->playEffect(fileName);
//	}
}

void CGameManager::stopSound()
{
	SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

bool CGameManager::isFirstRun()
{
	if(CCUserDefault::sharedUserDefault()->getIntegerForKey(FIRST_RUN) == 0)
		return true;
	else
		return false;
}

